package com.mallcloud.mall.product.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.common.core.util.ValidGroup;
import com.mallcloud.mall.common.excel.annotation.ResponseExcel;
import com.mallcloud.mall.product.api.entity.CategoryModel;
import com.mallcloud.mall.product.service.CategoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "分类信息管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/product/category")
public class CategoryController {
    private final CategoryService categoryService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('product_category_view')")
    public R<IPage<CategoryModel>> page(Page page, CategoryModel dto) {
        return R.ok(categoryService.page(page, Wrappers.query(dto)));
    }

    @ApiOperation("列表查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('product_category_view')")
    public R<List<CategoryModel>> list(CategoryModel dto) {
        return R.ok(categoryService.list(Wrappers.query(dto)));
    }

    @ApiOperation("返回树形菜单集合")
    @GetMapping("/tree")
    @PreAuthorize("@pms.hasPermission('product_category_view')")
    public R<List<Tree<Long>>> getTree(Long parentId, String name) {
        return R.ok(categoryService.selectTree(parentId, name));
    }

    @ApiOperation("通过id查询")
    @GetMapping("/getInfo")
    @PreAuthorize("@pms.hasPermission('product_category_view')")
    public R<CategoryModel> getInfo(CategoryModel dto) {
        return R.ok(categoryService.getById(dto.getCategoryId()));
    }

    @ApiOperation("新增")
    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('product_category_add')")
    public R<Boolean> save(@Validated({ValidGroup.Insert.class}) @RequestBody CategoryModel dto) {
        return R.ok(categoryService.saveCategory(dto));
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @PreAuthorize("@pms.hasPermission('product_category_edit')")
    public R<Boolean> updateById(@Validated({ValidGroup.Update.class}) @RequestBody CategoryModel dto) {
        return R.ok(categoryService.updateCategoryById(dto));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{categoryId}")
    @PreAuthorize("@pms.hasPermission('product_category_del')")
    public R<Boolean> removeById(@PathVariable Long categoryId) {
        return R.ok(categoryService.removeCategoryById(categoryId));
    }

    @ApiOperation("导出")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('product_category_export')")
    public List<CategoryModel> export(CategoryModel category) {
        return categoryService.list(Wrappers.query(category));
    }
}
